import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:joyagoo/utils/JoyCacheManager.dart';
import 'package:joyagoo/utils/router.dart';
import 'pages/joy_splash_page.dart';
import 'config/app_theme.dart';
import 'utils/KeyboardDismissOnTap.dart';
import 'utils/LanguageManager.dart';
import 'utils/LanguageMessage.dart';
import 'utils/category_manager.dart';


Future<void> main() async {
  // 确保Flutter绑定初始化
  WidgetsFlutterBinding.ensureInitialized();
  
  // 设置全局状态栏样式 - 黑色文字
  SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
    statusBarColor: Colors.transparent,
    statusBarIconBrightness: Brightness.dark, // Android 状态栏图标为深色
    statusBarBrightness: Brightness.light, // iOS 状态栏文字为深色
  ));

  // 初始化缓存管理器
  await JoyCacheManager.initCacheManager();
  
  // 初始化全局分类管理器
  CategoryManagerExtension.init();

  runApp(const JoyApp());
}

class JoyApp extends StatelessWidget {
  const JoyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      //屏幕适配
      designSize: const Size(360, 690),
      splitScreenMode: true,
      minTextAdapt: true,
      builder: (BuildContext context, child) {
        return KeyboardDismissOnTap(
            child: materialApp(
                child!
            ));
      },
      child: JoySplashPage(),
    );;
  }

  //app风格
  GetMaterialApp materialApp(Widget child) {
    return GetMaterialApp(
      locale: LanguageManager.instance.currentLocale,
      translations: LanguageMessage(),
      fallbackLocale: const Locale('en', 'US'),
      defaultTransition: Transition.rightToLeft,
      title: 'Litbuy Sheets',
      localizationsDelegates:  [
        GlobalWidgetsLocalizations.delegate,
        GlobalMaterialLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
      ],
      navigatorKey: Routers.navigatorKey,
      supportedLocales: const [
        Locale('zh', 'CN'),
        Locale('en', 'US'),
        Locale('ja', 'JP'),
        Locale('ko', 'KR'),
        Locale('fr', 'FR'),
        Locale('de', 'DE'),
        Locale('es', 'ES'),
        Locale('it', 'IT'),
        Locale('ru', 'RU'),
        Locale('ar', 'SA'),
      ],
      localeResolutionCallback:
          (Locale? locale, Iterable<Locale> supportedLocales) {
        // 如果当前语言在支持列表中，使用当前语言
        for (Locale supportedLocale in supportedLocales) {
          if (supportedLocale.languageCode == LanguageManager.instance.currentLanguage) {
            return supportedLocale;
          }
        }
        // 否则返回默认语言
        return const Locale('en', 'US');
      },
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        useMaterial3: false,
        // textTheme: Typography.englishLike2018.apply(fontSizeFactor: 1.sp),
        primaryColor: Colors.black,
        brightness: Brightness.dark,
        appBarTheme: AppBarTheme(
          backgroundColor: const Color(0xFF0D0F14),
          iconTheme: const IconThemeData(color: Colors.black),
          centerTitle: true,
          elevation: 0,
          titleTextStyle: TextStyle(
            color: Colors.black,
            fontSize: 16.sp,
            fontWeight: FontWeight.normal,
          ),
          systemOverlayStyle: const SystemUiOverlayStyle(
            statusBarColor: Colors.transparent,
            statusBarIconBrightness: Brightness.dark, // Android 状态栏图标为深色
            statusBarBrightness: Brightness.light, // iOS 状态栏文字为深色
          ),
        ),
      ),
      navigatorObservers:[
        FlutterSmartDialog.observer,
      ],
      home: child,
      builder: FlutterSmartDialog.init(builder: (BuildContext context, Widget? child){


        return MediaQuery(
          //设置文字大小不随系统设置改变
            data: MediaQuery.of(context),
            child: child!
        );
      }),
      enableLog:true,
      logWriterCallback:(String value, {bool isError = false}){
      },
    );
  }

}
